package cn.explink.dop.distributor.condition;

import java.util.List;

import cn.explink.distributor.framework.CommonCondition;
import cn.explink.distributor.framework.CommonTarget;
import cn.explink.distributor.framework.Condition;
import cn.explink.distributor.framework.Target;
import cn.explink.dop.gis.GeoCoder;
import cn.explink.dop.gis.GeoPoint;

public class DeliveryDistance extends ComparableCondition {

	private static final long serialVersionUID = 8700981616892423114L;

	private String storefrontCoordinate;

	private String deliverymanCoordinate;

	public DeliveryDistance() {
		super();
	}

	public DeliveryDistance(String storefrontCoordinate, String deliverymanCoordinate) {
		super();
		this.storefrontCoordinate = storefrontCoordinate;
		this.deliverymanCoordinate = deliverymanCoordinate;
	}

	@Override
	public boolean isComparableMatched(Condition orderCondition, List<Target> targetList) {
		String sfCoordinate = ((DeliveryDistance) orderCondition).getStorefrontCoordinate();

		double minDistance = GeoCoder.getInstance().getRouteMatrix().getRouteDistance(new GeoPoint(this.deliverymanCoordinate), new GeoPoint(sfCoordinate));

		for (Target target : targetList) {
			List<CommonCondition> distributeAbilityList = target.getDistributeAbilityList();
			for (CommonCondition distributeAbility : distributeAbilityList) {
				if (this.getClass().equals(distributeAbility.getClass())) {
					String dmCoordinate = ((DeliveryDistance) distributeAbility).getDeliverymanCoordinate();
					double distance = GeoCoder.getInstance().getRouteMatrix().getRouteDistance(new GeoPoint(dmCoordinate), new GeoPoint(sfCoordinate));
					if (distance < minDistance) {
						return false;
					}

				}
			}
		}
		return true;
	}

	public String getDeliverymanCoordinate() {
		return this.deliverymanCoordinate;
	}

	public void setDeliverymanCoordinate(String deliverymanCoordinate) {
		this.deliverymanCoordinate = deliverymanCoordinate;
	}

	public String getStorefrontCoordinate() {
		return this.storefrontCoordinate;
	}

	public void setStorefrontCoordinate(String storefrontCoordinate) {
		this.storefrontCoordinate = storefrontCoordinate;
	}

}
